//
//  SEChatImageCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/9.
//

import UIKit

let kChatImageMaxWidth: CGFloat = 125  // 最大的图片宽度
let kChatImageMinWidth: CGFloat = 50   // 最小的图片宽度
let kChatImageMaxHeight: CGFloat = 150 // 最大的图片高度
let kChatImageMinHeight: CGFloat = 50  // 最小的图片高度

class SEChatImageCell: SEChatBaseCell {

    override func update(with viewModel: SEChatViewModel) {
        super.update(with: viewModel)
        
        chatImageView.kf.indicatorType = .activity
        chatImageView.kf.setImage(urlString: viewModel.chatRecord.content)
        coverImageView.image = viewModel.bubbleCoverImage
        
        setNeedsLayout()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let vm = viewModel else { return }
        
        // 根据原图尺寸等比获取缩略图的 size
        let originalSize = CGSize(width: kChatImageMaxWidth, height: kChatImageMaxHeight)
        chatImageView.size = .getThumbImageSize(originalSize)
        
        if vm.fromMe {
            chatImageView.left = UIScreen.screenWidth - kChatAvatarMarginLeft - kChatAvatarWidth - kChatBubbleMarginLeft * 2 - chatImageView.width
            activityIndicator.right = chatImageView.left - kChatMargin
            resendButton.right = chatImageView.left - kChatMargin
            coverImageView.left = chatImageView.left
        } else {
            chatImageView.left = kChatBubbleLeft + kChatBubbleMarginLeft
            activityIndicator.left = chatImageView.right + kChatMargin
            resendButton.left = chatImageView.right + kChatMargin
            coverImageView.left = chatImageView.left - kChatBubbleMarginLeft - 2.5
        }
        
        chatImageView.top = avatarImageView.top
        activityIndicator.centerY = chatImageView.centerY
        resendButton.centerY = chatImageView.centerY
        coverImageView.top = chatImageView.top
        coverImageView.width = chatImageView.width + 2.5 + kChatBubbleMarginLeft
        coverImageView.height = chatImageView.height + 2
    }
    
    @objc private func imageDidTapped() {
        delegate?.imageCell(self, didTappedImageViewWith: viewModel)
    }
    
    @IBOutlet weak var chatImageView: UIImageView! {
        didSet {
            chatImageView.contentMode = .scaleAspectFill
            chatImageView.isUserInteractionEnabled = true
            chatImageView.layer.cornerRadius = 4
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageDidTapped))
            chatImageView.addGestureRecognizer(tapGesture)
        }
    }
    @IBOutlet weak var coverImageView: UIImageView!
}

// MARK: - Extension

private extension CGSize {
    /// 获取缩略图的尺寸
    /// - Parameter originalSize: 原始图的尺寸
    static func getThumbImageSize(_ originalSize: CGSize) -> CGSize {
        let imageOriginalWidth = originalSize.width
        let imageOriginalHeight = originalSize.height
        
        var resizeThumbWidth: CGFloat
        var resizeThumbHeight: CGFloat
        
        if imageOriginalHeight >= imageOriginalWidth {
            let scaleWidth = imageOriginalWidth * kChatImageMaxHeight / imageOriginalHeight
            resizeThumbWidth = (scaleWidth > kChatImageMinWidth) ? scaleWidth : kChatImageMinWidth
            resizeThumbHeight = kChatImageMaxHeight
        } else {
            let scaleHeight = imageOriginalHeight * kChatImageMaxWidth / imageOriginalWidth
            resizeThumbHeight = (scaleHeight > kChatImageMinHeight) ? scaleHeight : kChatImageMinHeight
            resizeThumbWidth = kChatImageMaxWidth
        }
        
        return CGSize(width: resizeThumbWidth, height: resizeThumbHeight)
    }
}

private extension CGRect {
    static func centerRectForResizableImage(_ image: UIImage) -> CGRect {
        return CGRect(x: image.capInsets.left / image.size.width,
                      y: image.capInsets.top / image.size.height,
                      width: (image.size.width - image.capInsets.left - image.capInsets.right) / image.size.width,
                      height: (image.size.height - image.capInsets.top - image.capInsets.bottom) / image.size.height)
    }
}
